package ivansalvadori.diariopushweb.dao.rdb;

import ivansalvadori.diariopushweb.dao.DiarioDAO;
import ivansalvadori.diariopushweb.model.Diario;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

public class RDBDiarioDAO extends SimpleJdbcDaoSupport implements DiarioDAO {

	@Override
	public List<Diario> listar() {
		List<Diario> diarios = getJdbcTemplate()
				.query("select codigo, nome_entidade from Diario order by nome_entidade",
						new RowMapper<Diario>() {
					@Override
					public Diario mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						Diario d = new Diario();
						d.setCodigo(rs.getInt("codigo"));
						d.setEntidade(rs.getString("nome_entidade"));
						return d;
					}
				});
		return diarios;
	}
}
